﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using com.pro2e.console.core.hibernate;
using NHibernate;
using com.pro2e.console.service.auth;
using com.pro2e.console.service.auth.vo;
using System.Web.SessionState;
using System.Collections.Generic;
using Quartz.Collection;
using com.pro2e.console.core.util;
using com.pro2e.web.util;

/// <summary>
/// AuthModule 的摘要描述
/// </summary>
/// 
namespace com.pro2e.web.auth
{
    public class AuthModule : IHttpModule, IRequiresSessionState
    {
        log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private readonly string MANAGE_LOGIN_PATH = "~/admin/login/Login.aspx";
        private readonly string MANAGE_NOAUTH_PATH = "~/admin/login/NoAuth.aspx";
        private readonly string MANAGE_ERROR = "~/admin/login/Error.aspx";
        public AuthModule()
        {

        }
        #region Fields and Properties

        #endregion

        #region IHttpModule Members

        public void Init(HttpApplication application)
        {
            application.AcquireRequestState += new EventHandler(Authorization);

        }

        public void Dispose() { }

        #endregion

        public void Authorization(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            Uri uri = application.Request.Url;

            string rawUrl = application.Request.RawUrl;

            //只欄aspx
            if (rawUrl.IndexOf("aspx") == -1)
            {
                return;
            }


            //登入頁面不檢查權限
            if (rawUrl.IndexOf("Login.aspx") != -1 || rawUrl.IndexOf("NoAuth.aspx") != -1 || rawUrl.IndexOf("leftmenu.aspx") != -1 || rawUrl.IndexOf("welcome.aspx") != -1 || rawUrl.IndexOf("Error.aspx") != -1 || rawUrl.IndexOf("toggle.aspx") != -1 || rawUrl.IndexOf("top.aspx") != -1 || rawUrl.IndexOf("connector.aspx")!=-1)
            {
                return;
            }

            log.Debug("Uri=" + uri.ToString());


           CheckAuth(application, uri, rawUrl);
        }

        /// <summary>
        /// 檢查權限
        /// </summary>
        /// <param name="application"></param>
        /// <param name="uri"></param>
        /// <param name="rawUrl"></param>
        private void CheckAuth(HttpApplication application, Uri uri, string rawUrl)
        {
            SessionHelper sHelper = new SessionHelper();

            LoginUser loginUser = sHelper.LoginUser;

            string applicationPath = application.Request.ApplicationPath;

            string mamagePath = String.IsNullOrEmpty(applicationPath) ? "/admin" : applicationPath + "/admin";
            mamagePath = mamagePath.Replace("//", "/");

            if (rawUrl.StartsWith(mamagePath) == true)
            {
             
                AuthService authService = new AuthService();

                if (loginUser == null)
                {
                    toLoginPage(application.Response);
                    return;
                }

                string userId = loginUser.UserId;

                //判斷只有主路徑是否有權限
                //if (!PathHasRight(UserMenuFuncContainer.GetInstance().GetUser(userId), uri, UserMenuFuncContainer.GetInstance().PathFunc))
                //{
                //    toLoginNoAuthPage(application.Response);
                //}


                //判斷所有路徑是否有權限                          
                if (!authService.PathHasAuth(UserMenuFuncContainer.GetInstance().GetUser(userId), uri))
                {
                    toLoginNoAuthPage(application.Response);
                }
            }
        }

     
        private bool PathHasRight(LoginUser loginUser, Uri uri, Dictionary<string, List<int>> pathFunc)
        {
            string url = uri.ToString();

            foreach (string path in pathFunc.Keys)
            {
                if (url.IndexOf(path) != -1)
                {
                    log.Fatal(path.IndexOf(url));

                    IList<int> funIdList = pathFunc[path];

                    //判斷是否有此功能權限

                    if (loginUser.BelongRoles != null && loginUser.BelongRoles.Count > 0)
                    {
                        foreach (LoginRole role in loginUser.BelongRoles)
                        {
                            if (role.MenuFuncs != null && role.MenuFuncs.Count > 0)
                            {
                                foreach (MenuFunc roleMenuFunc in role.MenuFuncs)
                                {
                                    foreach (int id in funIdList)
                                    {
                                        if (id == roleMenuFunc.Id)
                                        {
                                            return true;
                                        }
                                    }
                                }
                            }
                        }

                        //若未有權限 則丟回false
                        return false;
                    }
                    else
                    {
                        return false;
                    }
                }
            }

            return true;

        }

        private void toLoginPage(HttpResponse httpResponse)
        {
            httpResponse.Redirect(MANAGE_ERROR, false);
            return;
        }
        private void toLoginNoAuthPage(HttpResponse httpResponse)
        {
            httpResponse.Redirect(MANAGE_NOAUTH_PATH, false);
            return;
        }

    }
}